package 最大连续1的个数III;

class Solution {
    public int longestOnes(int[] nums, int k) {
        int len = 0;
        int left = 0;
        int right = 0;
        int count = k;
        int n = nums.length;
        while (right < n){
            while (left <= right && nums[right] == 0 && k == 0){
                if (nums[left] == 0 && count != 0){
                    left++;
                    k++;
                    break;
                }
                left++;
            }
            if (nums[right] == 0 && k > 0){
                k--;
            }
            len = Math.max(len,right - left + 1);
            right++;
        }
        return len;
    }
}